package com.lxc.dao;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.sql.DatabaseMetaData;
import com.lxc.common.PathManagement;
import java.io.*;
public class MusicTable{
    Statement statement = null;
    DatabaseMetaData metaData = null;
    public MusicTable(){
        try{
        Class.forName("com.mysql.cj.jdbc.Driver");
        }
        catch(Exception ex){
            System.out.println("创建数据库Driver失败！");
        }
        //获取连接数据库的基本信息
        Properties properties = new Properties();
		FileInputStream  in = null;
		try{
			in = new FileInputStream(PathManagement.getPropertiesPath()+"\\mysql.property");
        }
		catch(FileNotFoundException ex){
			System.out.println("获取数据库配置文件失败");
		}
		try{
		properties.load(in);
		}
		catch(IOException ex){
			System.out.println("加载配置文件失败");
		}
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String passw = properties.getProperty("password");
		
        try{
        Connection conn = DriverManager.getConnection(url,user,passw);
        metaData = conn.getMetaData();
        statement = conn.createStatement();
        }
        catch(SQLException ex){
            System.out.println("获取连接失败！");
			//ex.printStackTrace();
        }
    }
    public void addMusic(Map map){
		ResultSet columnSet = null;
		try{
        columnSet = metaData.getColumns(null,null,"musicmanagementsystem","%");
		while(columnSet.next()){
            System.out.println(columnSet.getString("COLUMN_NAME"));
        }
        }
		catch(SQLException ex){
			System.out.println("获取数据库元信息失败");
		}
		
        int id =(int) map.get("id");
        String singer =(String) map.get("singer");
        String songName =(String) map.get("song_name");
        String imageAddress = (String) map.get("image_address");
        String musicAddress = (String) map.get("music_address");
		imageAddress = changeCase(imageAddress);
		musicAddress = changeCase(musicAddress);
        String sqlString = "insert into musictable values( "+ id+",'"+singer + "','"+songName+"','"+imageAddress+"','" + musicAddress+"')";
        try{
		statement.executeUpdate(sqlString);
		}
		catch(SQLException ex){
			System.out.println("执行插入musictable数据失败");
			ex.printStackTrace();
		}
			
    }
    public Map getMusic(int id){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        String sqlString = "SELECT * FROM musictable WHERE id=" + id; 
		ResultSet resultSet = null;
		try{
         resultSet = statement.executeQuery(sqlString);
		}
		catch(SQLException ex){
			System.out.println("执行获取music数据失败");
			ex.printStackTrace();
		}
		String singer = null;
        String songName = null;
        String imageAddress = null;
        String musicAddress = null;
		try{
        resultSet.next();
		singer = resultSet.getString("singer");
		songName = resultSet.getString("song_name");
		imageAddress = resultSet.getString("image_address");
		musicAddress = resultSet.getString("music_address");
		}
		catch(SQLException ex){
			System.out.println("获取结果集失败");
		}
        //String idString = resultSet.getString("id");
        
        hashMap.put("id",id);
        hashMap.put("singer",singer);
        hashMap.put("song_name",songName);
        hashMap.put("image_address",imageAddress);
        hashMap.put("music_address",musicAddress);
        return hashMap;
    }
	private String changeCase(String s){
		char tempChar = ' ';
		String tempS = "";
		for(int i = 0; i< s.length(); i++){
			tempChar = s.charAt(i);
			if(tempChar == '\\'){
				tempS = tempS + "\\\\\\\\";
			}
			else{
				tempS =tempS + tempChar;
			}
		}
		return tempS;
	}
}